package com.hb.trade.api.routine.controller.project.calibration;

import cn.hutool.core.collection.CollUtil;
import com.hb.core.pojo.Attachment;
import com.hb.core.pojo.enums.NewsTypeEnum;
import com.hb.core.pojo.enums.NewsTypeTwoEnum;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.deal.dto.BidCandidateDTO;
import com.hb.core.pojo.project.deal.dto.BidCandidateSectionDTO;
import com.hb.core.pojo.project.deal.entity.BidCandidateSupplier;
import com.hb.trade.api.routine.service.PortalNoticeService;
import com.hb.trade.api.routine.sms.annotation.SmsSend;
import com.hb.trade.api.routine.util.SendNewsUtil;
import com.hb.trade.client.project.DealClient;
import com.hb.trade.client.project.SectionClient;
import fzzn.core.aspose.convert.AsposeConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 中标候选人公示API控制器
 *
 * @author Lix1ang
 * @date 2021-12-24 15:20:53
 */
@Slf4j
@Version(1.0)
@Api(tags = {"中标候选人公示"})
@RequestMapping("/{version}")
@RestController
public class ApiBidCandidateController {

    protected final DealClient dealClient;
    protected final SectionClient sectionClient;
    protected final PortalNoticeService portalNoticeService;

    public ApiBidCandidateController(DealClient dealClient,
                                     SectionClient sectionClient,
                                     PortalNoticeService portalNoticeService) {
        this.dealClient = dealClient;
        this.sectionClient = sectionClient;
        this.portalNoticeService = portalNoticeService;
    }

    @ApiOperation(value = "根据项目Id查询候选人公示列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目Id", dataTypeClass = Long.class)
    })
    @PostMapping("/candidate/getCandidateByProjectId")
    public ResponseDto getCandidateByProjectId(@RequestBody BidCandidateDTO bidCandidateDTO) {
        return ResultUtil.msg(dealClient.getCandidateByProjectId(bidCandidateDTO));
    }

    @ApiOperation(value = "根据标段id查询候选人公示", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = Long.class)
    })
    @PostMapping("/candidate/getCandidateBySectionId")
    public ResponseDto getCandidateBySectionId(@RequestBody BidCandidateDTO bidCandidateDTO) {
        return ResultUtil.msg(dealClient.getCandidateBySectionId(bidCandidateDTO));
    }

    @ApiOperation(value = "获取候选人公示详情", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键Id", dataTypeClass = Long.class)
    })
    @PostMapping("/candidate/getCandidateDetail")
    public ResponseDto getCandidateDetail(@RequestBody BidCandidateDTO bidCandidateDTO) {
        return ResultUtil.msg(dealClient.getCandidateDetail(bidCandidateDTO));
    }

    @ApiOperation(value = "新增或者修改中标候选人公示", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "projectId", value = "项目Id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "publicityName", value = "公示名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "publicityStartTime", value = "公示开始时间", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "publicityEndTime", value = "公示结束时间", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "publishMedium", value = "发布媒介", dataTypeClass = String.class),
            @ApiImplicitParam(name = "publicityContent", value = "公示内容", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pushFlag", value = "是否推送招投标服务平台(1:是   2:否)", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "attachment", value = "附件", dataTypeClass = Attachment.class),
            @ApiImplicitParam(name = "note", value = "备注", dataTypeClass = String.class),
            @ApiImplicitParam(name = "candidateSectionList", value = "标段关联关系列表", dataTypeClass = List.class)
    })
    @PostMapping("/candidate/saveOrUpdateCandidate")
    public ResponseDto saveOrUpdateCandidate(@RequestBody BidCandidateDTO bidCandidateDTO) {
        if (CollUtil.isNotEmpty(bidCandidateDTO.getPublicityContent())){
            Attachment attachment = bidCandidateDTO.getPublicityContent().get(0);
            File thisFile = attachment.getThisFile();
            String htmlStr = AsposeConvertUtil.wordToHtml(thisFile);
            bidCandidateDTO.setNoticeContent(htmlStr);

        }
        if (bidCandidateDTO.getId() == null){
            bidCandidateDTO.setCreateName(ShiroUtil.getUser().getFullName());
        }else {
            bidCandidateDTO.setUpdateName(ShiroUtil.getUser().getFullName());
        }
        Message message = dealClient.saveOrUpdateCandidate(bidCandidateDTO);
        return ResultUtil.msg(message);
    }

    @ApiOperation(value = "候选人发布", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键Id", dataTypeClass = Long.class)
    })
    @SmsSend(type = 9)
    @PostMapping("/candidate/publishCandidate")
    public ResponseDto publishCandidate(@RequestBody BidCandidateDTO bidCandidateDTO) {
        bidCandidateDTO.setDeployTime(LocalDateTime.now());
        Message message = dealClient.publishCandidate(bidCandidateDTO);
        Message<BidCandidateDTO> candidateDetail = dealClient.getCandidateDetail(bidCandidateDTO);
        //给投标人发送消息

        if (candidateDetail.isFail() ||candidateDetail.getData() == null){
            return ResultUtil.msg(message);
        }
        BidCandidateDTO bidCandidate = candidateDetail.getData();
        List<BidCandidateSectionDTO> candidateSectionList = bidCandidate.getCandidateSectionList();
        for (BidCandidateSectionDTO bidCandidateSectionDTO : candidateSectionList) {
            Message<List<Long>> longUserIds = sectionClient.listParticipateSection(bidCandidateSectionDTO.getSectionId());
            if (longUserIds.isFail() || CollUtil.isEmpty(longUserIds.getData())){
                continue;
            }
            List<Long> receiverIds = longUserIds.getData();
            //发送消息
            SendNewsUtil.sendBiddereeNews(
                    bidCandidateSectionDTO.getSectionId(),
                    NewsTypeEnum.PROJECT_CHANGE,
                    NewsTypeTwoEnum.NOTICE_PROJECT_CHANGE,
                    "中标候选人公示",
                    null,
                    "/console/candidateNotice",
                    receiverIds
            );
        }

        // 将中标候选人推送至门户
        portalNoticeService.pushCandidateToPortal(bidCandidateDTO.getId());

        return ResultUtil.msg(message);
    }

    @ApiOperation(value = "获取未被关联的标段", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键Id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "projectId", value = "项目Id", dataTypeClass = Long.class)
    })
    @PostMapping("/candidate/getSectionNotSelected")
    public ResponseDto getSectionNotSelected(@RequestBody BidCandidateDTO bidCandidateDTO) {
        return ResultUtil.msg(dealClient.getSectionNotSelected(bidCandidateDTO));
    }

    @ApiOperation(value = "获取中标候选人公示列表（从开标记录表里拉取投标人信息）", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "orgName", value = "企业名称", dataTypeClass = String.class)
    })
    @PostMapping("/candidate/getCandidate")
    public ResponseDto getCandidate(@RequestBody BidCandidateSupplier bidCandidateSupplier) {
        Message<PageResult> message = dealClient.getCandidate(bidCandidateSupplier);
        return ResultUtil.msg(message);
    }

    @ApiOperation(value = "根据候选人公示查询关联标段列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键Id", dataTypeClass = Long.class)
    })
    @PostMapping("/candidate/listSectionByCandidateId")
    public ResponseDto listSectionByCandidateId(@RequestBody BidCandidateDTO bidCandidateDTO) {
        return ResultUtil.msg(dealClient.listSectionByCandidateId(bidCandidateDTO));
    }
}
